<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }

    public function test() {
//        $baiduUrl = "http://news.sohu.com/20170404/n486448325.shtml";
//
//        file_get_contents($baiduUrl);
//        $responseInfo = $http_response_header;
//
//        print_r($responseInfo);
//        exit;


        $url = 'http://localhost:8088/index/index/test_header';
        $method = "GET";
        $appcode = "23b6b7fe2b9840168b2ce4bfe06c2075";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = 'aaa=111';
        $bodys = "";
        $url = $url . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$url, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result =  curl_exec($curl);

//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $url);
//        curl_setopt($ch, CURLOPT_HEADER, TRUE);    //表示需要response header
//        curl_setopt($ch, CURLOPT_NOBODY, FALSE); //表示需要response body
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
//        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
//        curl_setopt($ch, CURLOPT_TIMEOUT, 120);

//        $result = curl_exec($ch);

        return $result;
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            list($header, $body) = explode("\r\n\r\n", $result, 2);
        }
        dump($header);
        $data = explode("\r\n", $header);
        dump($data);

        foreach ($data as $loop) {
            if(strpos($loop, "aaa") !== false){
                $aaa = trim(substr($loop, 5));
                print_r($aaa);
// 输出: http://www.edeng.cn/s/chuna/
            }
        }
        return NULL;
    }

    public function test_header() {
        header('X-Powered-By: PHP/4.4.0');
        header('aaa: aaaa');

        $headers = getallheaders();
        $data = input('param.');
        file_put_contents('header.txt', json_encode($data) . " \n ");

        foreach ($headers as $k => $v) {
            file_put_contents('header.txt', $k . ' => ' . $v . " \n ", FILE_APPEND);
        }

        foreach ($_SERVER as $k => $v) {
            file_put_contents('header.txt', $k . ' => ' . $v . " \n ", FILE_APPEND);
        }
        //file_put_contents('header.txt', json_encode($_SERVER));
        return 123;
    }
}
